home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / groffvar.zoo / source / nroff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-06  |  2.5 KB  |  90 lines

  1. /* nroff.c - a replacement for the nroff shell script that comes          */
  2. /*           with the GNU distribution of groff 1.07                      */
  3. /* Written by Hildo Biersma on March 6, 1993                              */
  4. /* GNU public license applies - please see the file COPYING for details.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10.  
  11. /* These aren't defined in <unistd.h>, but should have been... */
  12. extern int  optind, opterr;
  13. extern char *optarg;
  14.  
  15. void main(int argc, char *argv[])
  16. {
  17.   char    **arg_vector;
  18.   int     arg_counter;
  19.   char    option, *ptr;
  20.   size_t  len;
  21.  
  22.   if ((arg_vector = malloc((argc + 5) * sizeof(char *))) == NULL)
  23.   {
  24.     fprintf(stderr, "%s: out of memory\n", argv[0]);
  25.     exit(1);
  26.   }
  27.   
  28.   arg_vector[0] = "groff";
  29.   arg_vector[1] = "-Wall";
  30.   arg_vector[2] = "-mtty-char";
  31.   arg_vector[3] = "-Tascii";
  32.   arg_counter = 4;
  33.   while ((option = getopt(argc, argv, "heqs:m:r:n:o:T:i")) != EOF)
  34.   {
  35.     switch(option)
  36.     {
  37.       case 'h':
  38.         arg_vector[arg_counter++] = "-P-h";
  39.         break;
  40.       case 'e':
  41.       case 'q':
  42.       case 's':
  43.         /* Ignore these options */
  44.         break;
  45.       case 'i':
  46.         arg_vector[arg_counter++] = "-i";
  47.         break;
  48.       case 'm':
  49.       case 'r':
  50.       case 'n':
  51.       case 'o':
  52.         /* Pass these arguments on, including their parameters */
  53.         len = strlen(optarg);
  54.         if ((ptr = malloc(len + 3)) == NULL)
  55.         {
  56.           fprintf(stderr, "%s: out of memory\n", argv[0]);
  57.           exit(1);
  58.         }
  59.         arg_vector[arg_counter++] = ptr;
  60.         ptr[0] = '-';
  61.         ptr[1] = option;
  62.         strncpy(ptr + 2, optarg, len);
  63.         ptr[len + 2] = 0x00;
  64.         break;
  65.       case 'T':
  66.         /* Only allow -Tascii and -Tlatin1 */
  67.         if (strcmp(optarg, "ascii") == 0)
  68.           arg_vector[3] = "-Tascii";
  69.         else if (strcmp(optarg, "latin1") == 0)
  70.           arg_vector[3] = "-Tlatin1";
  71.         /* ignore other -T options */
  72.         break;
  73.       default:
  74.         /* This should only get '?', for illegal options */
  75.         break;
  76.     } /* End of switch (options) */
  77.   } /* End of while (options left) */
  78.   
  79.   while (optind < argc)
  80.     arg_vector[arg_counter++] = argv[optind++];
  81.   arg_vector[arg_counter] = NULL;
  82.   
  83.   /* Now execute groff using the argument vector just built */
  84.   execvp("groff", arg_vector);
  85.   
  86.   /* This should never be reached */
  87.   fprintf(stderr, "%s: error in execvp()\n", argv[0]);
  88.   exit(1);
  89. } /* End of main() */
  90.